﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Entity.Table;
using SqlHelper;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class SearchInfoDatabase
    {
        /// <summary>
        /// 根据用户编号获取该用户的应缴费信息。
        /// </summary>
        /// <param name="filter">过滤条件</param>
        /// <returns></returns>
        public static DataSet GetPaymentInfoByUserID(string gasUserID,int znjdays)
        {
            DataSet ds = null;
            SqlConnection conn = null;

            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@UserID", SqlDbType.VarChar, gasUserID),
                                                            DbHelper.CreateCommandParameter("@ZnjDays",SqlDbType.Int,znjdays)};

            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                ds = DbHelper.GetDataSetByProcedure("NXS_GetPaymentInfoByUserID", paramList, conn);
            }
            catch (SqlException ex)
            {
                ds = null;
            }
            finally
            {
                conn.Close();
            }

            return ds;
        }

        /// <summary>
        /// 获取该用户的扣款标志
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static DataTable GetKouKuanFlag(string userID)
        {
            SqlConnection conn = null;
            DataTable dt = null;
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@UserID", SqlDbType.VarChar, userID) };
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                dt = DbHelper.GetTableByProcedure("NXS_GetKouKuanFlag", paramList, conn);
            }
            catch (SqlException ex)
            {
                dt = null;
            }
            finally
            {
                conn.Close();
            }

            return dt;
        }

        /// <summary>
        /// 获取该用户是否有其他费
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static bool IsOtherMoney(string userID)
        {
            SqlConnection conn = null;
            bool b = true;  //true代表有其它费，false:无其它费
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@UserID", SqlDbType.VarChar, userID) };
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                DataTable dt = DbHelper.GetTableByProcedure("GetOtherMoney", paramList, conn);
                if (dt != null && dt.Rows.Count > 0)
                {
                    double otherMoney = Convert.ToDouble(dt.Rows[0]["其他费"]);
                    b = otherMoney > 0;
                }
                else
                    b = false;
               
            }
            catch (SqlException ex)
            {
                b = true;
            }
            finally
            {
                conn.Close();
            }

            return b;
        }

        /// <summary>
        /// 通过编号获取某用户的缴费信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static DataTable GetChargeInfoByUserID(string userID)
        {
            SqlConnection conn = null;
            DataTable dt = null;
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@UserID", SqlDbType.VarChar, userID) };
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                dt = DbHelper.GetTableByProcedure("GetChargeInfoByUserID_Sp", paramList, conn);
            }
            catch (SqlException ex)
            {
                dt = null;
            }
            finally
            {
                conn.Close();
            }

            return dt;
        }
    }
}
